import { fail, internalServerError, ok } from '@/lib/request'
import { createTeam, getTeam } from '@/models/team'

export async function POST(request: any) {
  const payload = await request.json()
  try {
    // 先判断团队是否存在
    const teamExist = await getTeam(payload?.slug)
    if (teamExist) {
      return fail('该团队的 URL 重复')
    }

    // 创建团队
    const team = await createTeam(payload)
    return ok({
      name: team?.name,
      slug: team?.slug,
    })
  } catch (e: any) {
    return internalServerError()
  }
}
